package GUIMain;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;

import data.FilePath;

public class StatusLog {
	private static boolean initialized = false;
	private static JDialog dialog;
	private static JButton close;
	private static JList statusList;
	private static DefaultListModel dlm;
	private static JScrollPane jsp;
	private static GridBagConstraints gbc;
	
	public static void open(){
		if(!initialized){
			initialize();
		}
		GUIFocusManager.add(dialog);
	}
	private static void initialize(){
		dialog = new JDialog();
		close = new JButton("Close");
		dlm = new DefaultListModel();
		statusList = new JList(dlm);
		jsp = new JScrollPane(statusList);
		jsp.setMinimumSize(new Dimension(500,300));
		gbc = new GridBagConstraints();
		dialog.setLayout(new GridBagLayout());
		
		gbc.insets.left = 2;
		gbc.insets.right = 2;
		gbc.insets.top = 2;
		gbc.insets.bottom = 2;
		
		gbc.gridwidth = 1;
		gbc.gridheight = 1;
		
		gbc.gridx = 0;
		gbc.gridy = 0;
		dialog.add(new JLabel("Status Log"),gbc);
		gbc.gridy = 1;
		dialog.add(statusList,gbc);
		gbc.gridy = 2;
		dialog.add(close,gbc);
		
		
		GUIFocusManager.setDialogProperties(dialog);
		initialized = true;
	}
	public static void clear(){
		if(!initialized){
			initialize();
		}
		dlm.removeAllElements();
	}
	public static void addLine(String line){
		if(!initialized){
			initialize();
		}
		dlm.add(dlm.getSize(), line);
	}
	/**
	 * Saves log to external file for later review
	 */
	public static boolean saveLog(){
		try {
			FileWriter writer = new FileWriter(FilePath.ERROR_LOG_DIR);
			for(int i=0;i<dlm.size();i++){
				String line = (String)dlm.getElementAt(i);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}
